
$(function(){
    
    $('.login').on('click',function(){
        let data = {
            mobile:$('.text').val(),
            password:$('.password').val()
        }
        $.post(baseUrl+'/user/login',data,function(response){
            if(response.code != 0){
                layer.msg(response.msg);
                return false;
            }

            // 设置cookie保存用户信息
            let check = $('input[type="checkbox"]').prop('checked');
            if(check){
                $.cookie('useInfo', response.data, { expires: 7, path: '/' });
            }else{
                $.cookie('useInfo', response.data, { path: '/' });
            }

            // 实现购物车数据转移
            let userCart = localStorage.getItem('cart')?JSON.parse(localStorage.getItem('cart')):{};
            let carts = [];
            for(let key  in userCart){
                carts.push({
                    goodsId:key,
                    goodsCount:userCart[key]
                });
            }
            if(carts.length>0){
                // 开始转移
                $.post(baseUrl+'/cart/addAll',{userId:response.data.id,carts},(res)=>{
                    // 转移完成之后需要销毁本地的localstorage信息
                    localStorage.removeItem('cart');
                })
            }

            // h获取到回跳的地址
            let callback = getParam('callback');
            if(callback){
                location.href = decodeURIComponent(callback);
            }else{
                // 跳转首页
                location.href = 'index.html';
            }
            
        },'json')
    })
})

